/*定向交易*/
define(['jquery', 'api', 'user', 'util', 'text!pages/order/transfer.html', 'Vue'], function($, api, user, util, tpl, Vue) {
    var vm,
        time,
        startid,
        goodscode,
        // 应价类型:'1' - 挂单;'2' - 应价;'3' - 认购;'4' - 抢购;'5' - 大额配售;'6' - 划拨;
        TYPES = { '49': '挂单', '50': '应价', '51': '认购', '52': '抢购', '53': '大额配售', '54': '划拨' },
        controller = {
            init: function(code) {
                goodscode = code;
                var that = this;
                if (!user.checkLogin()) return;

                api.setTitle('定向交易');
                api.headerShow(1);
                api.headerTitle('定向交易');
                api.headerRightTitle('');
                api.headerRightLink('#/');
                api.footerShow(0);

                this.render();
            },
            render: function() {
                var that = this;
                $('#common').html('<div id="transfer"></div>');

                vm = new Vue({
                    el: '#transfer',
                    data: {
                        show: true,
                        ordersHistory: [], // 历史列表数据容器
                        ordersToday: [], // 今天列表数据容器
                        loading: false,
                        startTime: (function(d) { return d.format('yyyy-MM-dd') })(new Date(new Date().getTime() - 86400 * 1000)),
                        endTime: (function(d) { return d.format('yyyy-MM-dd') })(new Date()),
                        formGoodscode: goodscode,
                        formMobile: '',
                        formDirect: '',
                        formPrice: '',
                        formNum: '',
                        formVcode: '',
                        formConfirm: '',
                        currentOrder: {},
                        form: {},
                        initiate: true, // tab 我发起的
                        showMore: { // 显示更多
                            today: false,
                            history: false
                        },
                        noMoreNow: false,
                        noMoreHis: false
                    },
                    filters: {
                        lowHigh: util.lowHigh, //方向
                        //金额格式化
                        priceFormat: function(v, mindecimal) {
                            if (isNaN(v)) { return v = '' }; // 20161209dyx
                            return util.priceFormat(mindecimal, v);
                        },
                        typeName: function(o) {
                            var direct = '',
                                v = o.anspricetype;
                            if (TYPES[v] == undefined) {
                                return v;
                            }
                            if ((v == 49 || v == 50) && o.direction == 98) { //买入
                                direct = '买入';
                            } else if ((v == 49 || v == 50) && o.direction == 115) { //卖出
                                direct = '卖出';
                            }
                            return TYPES[v] + direct ? TYPES[v] + direct : '';
                        }
                    },
                    watch: {
                        formPrice: function(now, old) { // 监控挂单价格输入
                            if (isNaN(now) || parseFloat(now) <= 0) {
                                this.formPrice = 1;
                            } else {
                                this.formPrice = this.formPrice;
                            }
                        },
                        formNum: function(now, old) { // 监控挂单数量输入
                            if (!isNaN(now) || parseInt(now) > 0 || now == '') {
                                this.formNum = now ? parseInt(now) : '';
                            } else {
                                this.formNum = '';
                            }
                        }

                    },
                    methods: {
                        showAction: function() { //我发起的显示操作栏

                        },
                        submit: function() {
                            if (!this.formGoodscode) {
                                util.msg('未知商品');
                                return;
                            }
                            if (!this.formMobile) {
                                util.msg('请填写对方手机号');
                                return;
                            }
                            if (!this.formDirect || $.inArray(this.formDirect, [98, 115]) == -1) {
                                util.msg('请选择方向');
                                return;
                            }
                            if (isNaN(this.formPrice) || this.formPrice <= 0) {
                                util.msg('请填写价格');
                                return;
                            }
                            if (isNaN(this.formNum) || this.formNum < 1) {
                                util.msg('请填写数量');
                                return;
                            }
                            if (isNaN(this.formVcode) || this.formVcode == '') {
                                util.msg('请填写效验码');
                                return;
                            }
                            if (!this.formConfirm) {
                                util.msg('请阅读并同意《定向交易协议》');
                                return;
                            }

                            //=57&goodscode=&direct=&orderprice=&mobile=&checkcode=
                            var data = { goodscode: this.formGoodscode, orderprice: this.formPrice, mobile: this.formMobile, checkcode: this.formVcode, orderamount: this.formNum };
                            api.transferSub(
                                data,
                                function(rs) { //suc
                                    console.log(rs);
                                    if (rs.retcode == '0') {
                                        util.msg('提交成功！');
                                        //重置表单
                                        vm.resetForm();
                                    } else {
                                        util.dealErrorCode(rs.retcode);
                                    }
                                }
                            );
                        },
                        resetForm: function() {
                            this.formMobile = '';
                            this.formDirect = '';
                            this.formPrice = '';
                            this.formNum = '';
                            this.formVcode = '';
                            this.formConfirm = '';
                        },
                        direct: function(d) { //方向选择
                            this.formDirect = d;
                        },
                        switchTab: function(t) {
                            this.initiate = t;
                            if (t) { //我发起的
                                that.loadInitiate();
                            } else { //我收到的
                                that.loadReceive();
                            }
                        }
                    },
                    template: tpl
                }); // end vue

                that.loadInitiate();

            },
            loadInitiate: function() { //我发起的

            },
            loadReceive: function() { //我收到的

            },
            onRouteChange: function() {
                if (vm) vm.show = false;
            }
        };
    return controller;
})